class Card(suit:Symbol,value:Int) {
  def getSuit = this.suit.name
  def getValue = this.value
  override def toString = getValue + " " + getSuit
}
object Hand {
  def newHand:Array[Card] = {
    val hand:Array[Card] = new Array[Card](0)
    hand
  }
  def addCard(hand:Array[Card],card:Card):Array[Card] = {
    val newHand:Array[Card] = new Array[Card](hand.length + 1)
    hand.foreach(c => {
      newHand(hand.indexOf(c)) = c
    })
    newHand(newHand.length - 1) = card
    newHand
  }
  def removeCard(hand:Array[Card],card:Card):Array[Card] = {
    val newHand:Array[Card] = new Array[Card](hand.length - 1)
    var index = 0
    hand.foreach(c => {
      if(c != card) {
	newHand(index) = c
	index += 1
      }})
    newHand
  }
}
object Player {
  def newPlayer(name:String,num:Int,h:Array[Card]):(String,Int,Array[Card]) = {
    (name, num, h)
  }
  def newPlayer(name:String,num:Int):(String,Int,Array[Card]) = {
    (name, num, Hand.newHand)
  }
  def getName(player:(String,Int,Array[Card])):String = {player._1}
  def getNumber(player:(String,Int,Array[Card])):Int = {player._2}
  def getHand(player:(String,Int,Array[Card])):Array[Card] = {player._3}
  def recieveCard(p:(String,Int,Array[Card]),c:Card):(String,Int,Array[Card])= {
    val newHand = Hand.addCard(Player.getHand(p),c)
    Player.newPlayer(Player.getName(p), Player.getNumber(p), newHand)
  }
  def giveCard(p:(String,Int,Array[Card]),i:Int):(String,Int,Array[Card]) = {
    val newHand = Hand.removeCard(Player.getHand(p),Player.getHand(p)(i))
    Player.newPlayer(Player.getName(p), Player.getNumber(p),newHand)
  }
  def xCard(p1:(String,Int,Array[Card]), p2:(String,Int,Array[Card]),c:Int):((String,Int,Array[Card]),(String,Int,Array[Card])) = {
    val card = Player.getHand(p1)(c)
    println("Exchanged card: " + card)
    val p1NewHand = Hand.removeCard(Player.getHand(p1),card)
    val p2NewHand = Hand.addCard(Player.getHand(p2),card)
    val p1New = Player.newPlayer(Player.getName(p1),Player.getNumber(p1),p1NewHand)
    val p2New = Player.newPlayer(Player.getName(p2),Player.getNumber(p2),p2NewHand)
    (p1New,p2New)
  }
}
object Deck {
  val suits = List('Club,'Diamond,'Heart,'Spade)
  def newDeck:Array[Card] = {
    val deck:Array[Card] = new Array[Card](52)
    var index = 0
    for(suit <- suits; i <- 1 to 13) {
      deck(index) = new Card(suit,i)
      index += 1
    }
    return deck
  }
  def shuffle(deck:Array[Card]):Array[Card] = {
    val rand = scala.util.Random
    for(card <- deck) {
      val i = rand.nextInt(deck.length - deck.indexOf(card))
      swapCards(deck,deck.indexOf(card),i)      
    }
    deck
  }
  private def swapCards(deck:Array[Card],c1:Int,c2:Int) {
    val temp = deck(c1)
    deck(c1) = deck(c2)
    deck(c2) = temp
    deck
  }
}

object Main {
  val debug = true
  def main(args:Array[String]) {
    val players = new Array[(String,Int,Array[Card])](2)
    players(0) = Player.newPlayer("Steve",0)
    players(1) = Player.newPlayer("Andrew",1)
    val cards = List(new Card('Spade,1),new Card('Spade,2), new Card('Spade,3))
    cards.foreach(c => {
      players(0) = Player.recieveCard(players(0),c)})
    val tempPlayers = Player.xCard(players(0),players(1),0)
    players(0) = tempPlayers._1
    players(1) = tempPlayers._2
    println("post exchange")
    println("Player 0")
    (Player.getHand(players(0))).foreach(println)
    println("player 1")
    (Player.getHand(players(1))).foreach(println)
  }
}
